package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("")
    @RequiresPermissions(":index")
    @PermissionName("部门列表")
    public String index() {
        //只负责跳转到department.jsp页面即可
        return "department";
    }

    @RequestMapping("list")
    @RequiresPermissions("department:list")
    @PermissionName("部门数据")
    @ResponseBody
    public PageResult list(QueryObject qo) {
        PageResult result = departmentService.query(qo);
        return result;
    }

    @RequestMapping("changeState")
    @RequiresPermissions("department:changeState")
    @PermissionName("部门离职")
    @ResponseBody
    public JsonResult quit(Long id) {
        JsonResult result = new JsonResult();
        try {
            departmentService.updateState(id);
            result.setMessage("离职成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("离职失败");
        }
        return result;
    }

    @RequestMapping("save")
    @RequiresPermissions("department:save")
    @PermissionName("部门工保存")
    @ResponseBody
    public JsonResult save(Department department) {
        JsonResult result = new JsonResult();
        try{
            departmentService.insert(department);
            result.setMessage("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }

    @RequestMapping("edit")
    @RequiresPermissions("department:edit")
    @PermissionName("部门编辑")
    @ResponseBody
    public JsonResult edit(Department department) {
        JsonResult result = new JsonResult();
        try{
            departmentService.updateByPrimaryKey(department);
            result.setMessage("编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }
    
}